﻿using SCM.Common;
using SCM.Common.Extensions;
using SCM.Model;
using SCM.Model.DBEntity;
using SCM.Servie.IBusiness;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Linq;
using SqlSugar;

namespace SCM.Servie.Business
{
    /// <summary>
    /// 组件明细服务
    /// </summary>
    public class MaterialSubProductDtlService : IMaterialSubProductDtlService
    {
        private readonly DbContext dal = new DbContext();

        public string Add(T_Material_SubProductDtl model)
        {
            return dal.Insertable(model).ExecuteReturnEntity().ID;
        }


        public bool Delete(string materialSubProductId)
        {
            return dal.Updateable<T_Material_SubProductDtl>(new
            {
                DataStatus = DataStatusEnum.Deleted,
                ModifyTime = DateTime.Now,
            }).Where(p => p.MaterialSubProductID == materialSubProductId).ExecuteCommand() > 0;
        }

        public bool Save(string materialSubProductId, List<T_Material_SubProductDtl> list)
        {
            try
            {
                dal.BeginTran();

                var Ids = list.Select(p => p.ID).ToList();
                dal.Updateable<T_Material_SubProductDtl>(new
                {
                    DataStatus = DataStatusEnum.Deleted,
                }).Where(p => p.MaterialSubProductID == materialSubProductId && !Ids.Contains(p.ID)).ExecuteCommand();

                bool result = dal.SaveOrUpdate(list) > 0;
                if (result == false)
                {
                    dal.RollbackTran();
                }

                dal.CommitTran();
                return true;
            }
            catch
            {
                dal.RollbackTran();
                return false;
            }
        }

        public T_Material_SubProductDtl GetById(string id)
        {
            return dal.Get<T_Material_SubProductDtl>(id);
        }

        public List<T_Material_SubProductDtl> GetList(string materialSubProductID)
        {
            Expression<Func<T_Material_SubProductDtl, bool>> whereExp = (a) => a.DataStatus == DataStatusEnum.Valid;
            if (!string.IsNullOrWhiteSpace(materialSubProductID))
            {
                whereExp = whereExp.Compose((a) => a.MaterialSubProductID == materialSubProductID, Expression.AndAlso);
            }
            return dal.Queryable<T_Material_SubProductDtl>().Where(whereExp).ToList();
        }

        public PageData<T_Material_SubProductDtl> Query(string materialSubProductID, PageInfo page)
        {
            int count = 0;
            Expression<Func<T_Material_SubProductDtl, bool>> whereExp = (a) => a.DataStatus == DataStatusEnum.Valid;
            if (!string.IsNullOrWhiteSpace(materialSubProductID))
            {
                whereExp = whereExp.Compose((a) => a.MaterialSubProductID == materialSubProductID, Expression.AndAlso);
            }
            List<T_Material_SubProductDtl> list = dal.Queryable<T_Material_SubProductDtl>().Where(whereExp).ToPageList(page.PageIndex, page.PageSize, ref count);
            page.DataCount = count;
            return new PageData<T_Material_SubProductDtl> { Data = list, Page = page };
        }

        public bool Update(T_Material_SubProductDtl model)
        {
            return dal.Modify(model);
        }

        public List<MaterialSubProductDtlVo> GetListVo(string materialSubProductID)
        {
            Expression<Func<T_Material_SubProductDtl, T_Material_Parts, bool>> whereExp = (a, b) => a.DataStatus == DataStatusEnum.Valid && a.MaterialSubProductID == materialSubProductID;
            return dal.Queryable<T_Material_SubProductDtl, T_Material_Parts>((a, b) => new object[]
            {
                JoinType.Left,a.MaterialPartsID == b.ID,
            })
            .Where(whereExp)
            .Select((a, b) => new MaterialSubProductDtlVo()
            {
                ID = a.ID,
                MaterialSubProductID = a.MaterialSubProductID,
                MaterialPartsID = a.MaterialPartsID,
                PartName = b.PartName,
                PartCode = b.PartCode,
                Qty = a.Qty,
                DataStatus = a.DataStatus
            }).ToList();
        }
    }
}
